package br.puc.casas.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.DisposableBean;

public class HibernateHelper implements DisposableBean {

	private static Session session = null;

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession() {
		if (session == null) {
			session = sessionFactory.openSession();
		}

		return session;
	}

	public void destroy() throws Exception {
		if (session != null) {
			session.flush();
			session.close();
			session = null;
		}
	}	
}
